<!-- 弹窗用户 -->

<template>
  <div class="marginBottom">
    <BtnPrimary title="新增成员" :disabled="isShow" @click="toOpen" />

    <EasyModal v-model="easyVisible" title="选择小组成员" large @submit="toSubmit" @close="toReset">
      <ChooseUser :obj="obj" bind="user" hasMobile />
    </EasyModal>
  </div>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'ModalUser',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb()],

  /**
   * 状态数据 (函数返回对象)
   */
  data() {
    return {
      easyVisible: false, // 弹窗是否可见
      obj: { user: [] }, // 数据对象
    }
  },

  /**
   * 监听属性 (深度 deep 首次 immediate)
   */
  watch: {
    /**
     * 监听弹窗可见
     */
    easyVisible(visible) {
      visible && this.toReset()
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 弹窗
     */
    toOpen() {
      this.easyVisible = true
    },

    /**
     * 提交
     */
    toSubmit() {
      const { obj, one } = this
      one.emergencyMemberDto = obj.user.map((item) => {
        const organId = item.organId.at(0) || ''
        const organ = CORE.ORGAN.MAP[organId] || {}
        return {
          memberUaaUserId: item.uaaUserId,
          memberUserName: item.name,
          memberUserPhone: item.phone,
          organizationId: organId,
          organizationName: organ.name || '',
          isLeader: item.uaaUserId === one.leaderId ? 1 : 2,
        }
      })
      this.easyVisible = false
    },

    /**
     * 重置
     */
    toReset() {
      const user = this.one.emergencyMemberDto.map((item) => ({
        uaaUserId: item.memberUaaUserId,
        name: item.memberUserName,
        phone: item.memberUserPhone,
        organId: item.organizationId.split(','),
        organ: item.organizationName,
      }))
      this.obj = { user }
    },

    /**
     * 清除
     */
    toClear() {
      this.obj = { user: [] }
    },
  },
}
</script>
